<?php

require_once('../../../../wp-load.php');

global $wpdb;
if(isset($_GET["action"]) && strcmp($_GET["action"], "suggestion_tag") == 0){
	$s= $_GET["key"];
	$results = $wpdb->get_col( $wpdb->prepare( "SELECT t.name FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = 'post_tag' AND t.name LIKE (%s)",'%' . like_escape( $s ) . '%' ) );
	echo json_encode($results); 
	exit;
}

if(isset($_GET["action"]) && strcmp($_GET["action"], "delete_post") == 0){
	if(isset($_GET["post_id"]) &&  !empty($_GET["post_id"])){
		$postId= $_GET["post_id"];
		$rs =  wp_delete_post($postId);
		if($rs == False ){
			echo json_encode('{"rs": "0"}');
			exit;
		}else{
			echo json_encode('{"rs": "1"}');
			exit;
		}
		
	}
	echo json_encode('{"rs": "2"}');
	exit;

}


//$content = strip_tags($input);

$isUpdate = false;

if(isset($_POST["user_ID"]) &&  !empty($_POST["user_ID"])){
	$listCategory = explode(",", $_POST["post_category"]);
	if(isset($_POST["post_id"]) &&  !empty($_POST["post_id"])){
		$isUpdate = true;
		$post = array(
	 		'ID'             => $_POST["post_id"], //Are you updating an existing post?
			'post_author'    => $_POST["user_ID"], //The user ID number of the author.
			'post_category'  => $listCategory,//post_category no longer exists, try wp_set_post_terms() for setting a post's categories
			'post_content'   => $_POST["content"], //The full text of the post.
			'post_status'    => 'publish',
			'post_title'     => $_POST["post_title"] ,
			'post_type'      => 'post',
			'tags_input'     => $_POST["post_tags"] , //For tags.
// 			'to_ping'        => [ ? ] //?
// 			'tax_input'      => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies.
			);
	}else{
		$post = array(
	// 		'ID'             => [ <post id> ] //Are you updating an existing post?
	// 		'menu_order'     => [ <order> ] //If new post is a page, it sets the order in which it should appear in the tabs.
	// 		'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments.
	// 		'ping_status'    => [ 'closed' | 'open' ] // 'closed' means pingbacks or trackbacks turned off
	// 		'pinged'         => [ ? ] //?
			'post_author'    => $_POST["user_ID"], //The user ID number of the author.
			//'post_category'  => [ array(<category id>, <...>) ] //post_category no longer exists, try wp_set_post_terms() for setting a post's categories
			'post_content'   => $_POST["content"], //The full text of the post.
	// 		'post_date'      => [ Y-m-d H:i:s ] //The time post was made.
	// 		'post_date_gmt'  => [ Y-m-d H:i:s ] //The time post was made, in GMT.
	// 		'post_excerpt'   => [ <an excerpt> ] //For all your post excerpt needs.
	// 		'post_name'      => [ <the name> ] // The name (slug) for your post
	// 		'post_parent'    => [ <post ID> ] //Sets the parent of the new post.
	// 		'post_password'  => [ ? ] //password for post?
			'post_status'    => 'publish',
			'post_category'  => $listCategory,//post_category no longer exists, try wp_set_post_terms() for setting a post's categories
			'post_title'     => $_POST["post_title"] ,
			'post_type'      => 'post',
			'tags_input'     => $_POST["post_tags"] , //For tags.
// 			'to_ping'        => [ ? ] //?
// 			'tax_input'      => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies.
			);
}
	//insert to database
$post_id = wp_insert_post($post);
	//redirect after insert successfull
if($isUpdate){
	wp_redirect( get_site_url()."?p=".$post_id."&update=ok");	
}else{
	wp_redirect( get_site_url()."?p=".$post_id);	
}


}

?>